package com.linko.intelligent.sys.core.controller;

import com.github.pagehelper.PageInfo;
import com.linko.intelligent.base.CommonResponse;
import com.linko.intelligent.sys.core.service.NoticeService;
import com.linko.intelligent.sys.pojo.dto.NoticeDto;
import com.linko.intelligent.sys.pojo.po.Notice;
import com.linko.intelligent.sys.pojo.query.NoticeQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@RestController
@RequestMapping("/notice")
@Api(value = "NoticeController", tags = "通知")
public class NoticeController {

    @Autowired
    NoticeService noticeService;


    /***
     * 新增
     * @param dto
     * @return
     */
    @ApiOperation("新增")
    @PostMapping(value = "/save")
    public CommonResponse save(@RequestBody NoticeDto dto) {
        noticeService.save(dto);
        return new CommonResponse(CommonResponse.SUCCESS, "保存成功");
    }

    /***
     * 新增
     * @param dtoList
     * @return
     */
    @ApiOperation("新增List")
    @PostMapping(value = "/saveList")
    public CommonResponse saveList(@RequestBody List<NoticeDto> dtoList) {
        noticeService.saveList(dtoList);
        return new CommonResponse(CommonResponse.SUCCESS, "保存成功");
    }

//    @ApiOperation("删除")
//    @DeleteMapping(value = "/{id}/del")
//    public CommonResponse del(@PathVariable("id") Long id) {
//        noticeService.delete(id);
//        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
//    }
//
//    @ApiOperation("修改")
//    @PutMapping(value = "/{id}/update")
//    public CommonResponse update(@PathVariable("id") Long id, @RequestBody NoticeDto dto) {
//        noticeService.update(id, dto);
//        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
//    }

    @ApiOperation("详情")
    @GetMapping(value = "/{id}")
    public CommonResponse<Notice> get(@PathVariable("id") Long id) {
        Notice po = noticeService.getById(id);
        return new CommonResponse<Notice>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("详情-关联内容")
    @GetMapping(value = "/{id}/detail")
    public CommonResponse<Notice> detail(@PathVariable("id") Long id) {
        Notice po = noticeService.getDetail(id);
        return new CommonResponse<Notice>(CommonResponse.SUCCESS, "成功", po);
    }

    @ApiOperation("列表")
    @GetMapping(value = "/list")
    public CommonResponse<List<Notice>> list(@SpringQueryMap NoticeQuery query) {
        List<Notice> list = noticeService.queryList(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功", list);
    }

    /***
     * @param query
     * @return
     */
    @ApiOperation("分页")
    @GetMapping(value = "/page")
    public CommonResponse<PageInfo<Notice>> page(@SpringQueryMap NoticeQuery query) {
        PageInfo<Notice> pageInfo = noticeService.queryPage(query);
        return new CommonResponse<>(CommonResponse.SUCCESS, "查询成功!", pageInfo);
    }

    @ApiOperation("已读")
    @PutMapping(value = "/{id}/read")
    public CommonResponse read(@PathVariable("id") Long id) {
        if (noticeService.getById(id) == null) {
            throw new RuntimeException("找不到对应消息");
        }
        NoticeDto noticeDto = new NoticeDto();
        noticeDto.setStatus(1L);
        noticeDto.setReadTime(new Date());
        noticeService.update(id, noticeDto);
        return new CommonResponse<>(CommonResponse.SUCCESS, "成功");
    }

}
